#!/usr/bin/python
# encoding=utf-8
from SshClient import SshClient
import os
import time
import threading
#import paramiko
#ssh = paramiko.SSHClient()
count=2
serverSSHs=[
	SshClient(host='10.211.55.14', port=22, username='chenxin', password='asdfasdf')
]
'''
mysql-common_5.7.17-1ubuntu14.04_amd64.deb 
libmysqlclient20_5.7.17-1ubuntu14.04_amd64.deb 
libmysqlclient-dev_5.7.17-1ubuntu14.04_amd64.deb 
libmysqld-dev_5.7.17-1ubuntu14.04_amd64.deb
mysql-community-client_5.7.17-1ubuntu14.04_amd64.deb
mysql-client_5.7.17-1ubuntu14.04_amd64.deb 
mysql-community-server_5.7.17-1ubuntu14.04_amd64.deb
'''
def init():	
	cmds=[
		'wget http://mirrors.sohu.com/mysql/MySQL-5.7/mysql-server_5.7.17-1ubuntu14.04_amd64.deb-bundle.tar',
		'tar -xvf mysql-server_5.7.17-1ubuntu14.04_amd64.deb-bundle.tar',
		'sudo apt-get install  -y libaio1 libmecab2',
		'sudo dpkg -i mysql-common_5.7.17-1ubuntu14.04_amd64.deb libmysqlclient20_5.7.17-1ubuntu14.04_amd64.deb libmysqlclient-dev_5.7.17-1ubuntu14.04_amd64.deb libmysqld-dev_5.7.17-1ubuntu14.04_amd64.deb mysql-community-client_5.7.17-1ubuntu14.04_amd64.deb mysql-client_5.7.17-1ubuntu14.04_amd64.deb mysql-community-server_5.7.17-1ubuntu14.04_amd64.deb']
	for index,value in enumerate(serverSSHs):
		ret=serverSSHs[index].sexecute('ls|grep mysql|wc -l')
		res=int(ret['out'][0].replace("\n",""))
		if res<1:
			for i in range(0, len(cmds)):
				ret=value.sexecute(cmds[i],True)

	#备份配置文件
	for index,value in enumerate(serverSSHs):
	    ret=value.execute("sudo cp /etc/mysql/mysql.conf.d/mysqld.cnf /etc/mysql/mysql.conf.d/mysqld.cnf_back",True)
	print(ret)
	#   
	#serverSSH2.execute("cp /etc/mysql/mysql.conf.d/mysqld.cnf /etc/mysql/mysql.conf.d/mysqld.cnf_back")    

def changeConf(ser,sid):
	ser.execute("sudo rm /etc/mysql/mysql.conf.d/mysqld.cnf",True)
	ser.execute("sudo cp /etc/mysql/mysql.conf.d/mysqld.cnf_back /etc/mysql/mysql.conf.d/mysqld.cnf",True);
	#修改配置文件
	str=ser.sexecute('cat /etc/mysql/mysql.conf.d/mysqld.cnf')

	if len(str['out'])>0:
		for i in range(0, len(str['out'])):            
			str['out'][i]=str['out'][i].replace("127.0.0.1","0.0.0.0")
			
	str['out']+='log-bin=mysql-bin\nserver-id='+sid+'\n'
	t=ser.sexecute('sudo sh -c \'echo "%s">/etc/mysql/mysql.conf.d/mysqld.cnf\''%''.join(str['out']))
	#重启服务
	ser.execute('sudo /etc/init.d/mysql restart')
	print 'changeConf1 end'


def m2s():
	print "m2s start"
	#判断mysql是否启动成功
	lastPid=serverSSH1.execute('pgrep mysqld')
	if len(lastPid['out']) < count:
	   print "< 2"
	   timer = threading.Timer(5,m2s)
	   timer.start()
	  
	else:     
		print "> 2"
	#查询master的状态
	#serverSSH1.sexecute('mysql -uroot -p123456')
		serverSSH1.sexecute('mysql -uroot -p123456 -e "use mysql; GRANT REPLICATION SLAVE ON *.* to \\"mysync\\"@\\"%\\" identified by \\"q123456\\";" ')
		t= serverSSH1.sexecute('mysql -uroot -p123456 -e "show master status;" ')
		info= t['out'][1].split( )
		s_mysqlbin = info[0]
		s_position = info[1]
		serverSSH2.sexecute('mysql -uroot -p123456 -e "use mysql; update user set host = \\"%\\" where user =\\"root\\";  flush privileges; " ')
		serverSSH2.sexecute('mysql -uroot -p123456 -e "use mysql; change master to master_host=\\"'+server1 +'\\",master_user=\\"mysync\\",master_password=\\"q123456\\",master_log_file=\\"'+s_mysqlbin + '\\",master_log_pos='+s_position+'; " ')
		serverSSH2.sexecute('mysql -uroot -p123456 -e "start slave;"')
	print "m2s end"

if __name__ == '__main__':
	init()
	for index,value in enumerate(serverSSHs):
	    index=index+1
	    changeConf(value,str(index))
   #m2s()